//
//  PrescriptionOrderBottomPayView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/3.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol DrugsPayBottomViewDelegate: NSObjectProtocol {
    func didTappedPayAction(type: NewAdvisoryGenerateOrderEnum, view: PrescriptionOrderBottomPayView)
}

class PrescriptionOrderBottomPayView: UIView {
    
    func showTestMedicalInsurancePayBtn()  {
        medicalInsurancePayBtn.isHidden = true
        tips.isHidden = true
    }

    var cardType: CardType = .unbound {
        didSet {
            medicalInsurancePayBtn.isHidden = true
            tips.isHidden = true
            switch cardType {
            case .unbound: break
            case .medicare:
                medicalInsurancePayBtn.isHidden = false
                tips.isHidden = false
            case .selfpaid: break
            case .double:
                medicalInsurancePayBtn.isHidden = false
                tips.isHidden = false
            }
        }
    }
    
    weak var delegate: DrugsPayBottomViewDelegate?
    
    private var payTimeout: Bool = false
    
    @IBOutlet weak var totalCostLabel: UILabel!
    
    @IBOutlet weak var timeLabel: UILabel!
    /// 毫秒时间戳
    var timeStamp: Int = 0
    private var timer: YSGTimer!
    
    deinit {
        if timer != nil {
            timer.invalidate()
        }
    }
    
    private func setupTimer() {
        if timer == nil {
            timer = YSGTimer.scheduledTimer(with: 1, repeats: true, block: { [weak self] _ in
                self?.timerRun()
            })
            timer.fire()
        }
    }
    
    private func timerRun() {
        timeStamp -= 1
        if timeStamp <= 0 {
            let str = "已超时"
            let attributes = [NSAttributedString.Key.foregroundColor: UIColor.warning]
            let attrStr = NSMutableAttributedString(string: str)
            attrStr.setAttributes(attributes, range: NSRange(location: 0, length: str.count))
            timeLabel.attributedText = attrStr
            timer.invalidate()
        }
        
        let timeStr = Date.timeStampToString(timeStamp: timeStamp)
        let attrStr = NSMutableAttributedString(string: "请在\(timeStr)内完成支付")
        let attributes = [NSAttributedString.Key.foregroundColor: UIColor(hex: "#E99F0A")]
        attrStr.setAttributes(attributes, range: NSRange(location: 2, length: timeStr.count))
        timeLabel.attributedText = attrStr
    }
    
    func bind(to viewModel: PrescriptionOrderDetailViewModel) {
        totalCostLabel.text = viewModel.totalPrice
        timeStamp = viewModel.remainPayTime.intValue
        timeLabel.attributedText = viewModel.remainPayTimeAttr
        payTimeout = viewModel.payTimeout
        medicalInsurancePayBtn.isEnabled = !payTimeout
        wechatPayBtn.isEnabled = !payTimeout
        cardType = viewModel.cardType
        if payTimeout { return }
        
        setupTimer()
    }
    
    @IBOutlet weak var medicalInsurancePayBtn: UIButton! {
        didSet {
            medicalInsurancePayBtn.setTitle("医保支付", for: .normal)
            medicalInsurancePayBtn.setTitleColor(.white, for: .normal)
            medicalInsurancePayBtn.backgroundColor = .mainColor
            medicalInsurancePayBtn.titleLabel?.font = .regular_12
            medicalInsurancePayBtn.layer.cornerRadius = 15
            medicalInsurancePayBtn.layer.masksToBounds = true
            medicalInsurancePayBtn.setBackgroundImage(UIImage(color: .splitLineLight), for: .disabled)
            medicalInsurancePayBtn.isHidden = true
        }
    }
    @IBOutlet private weak var wechatPayBtn: UIButton! {
        didSet {
            wechatPayBtn.setTitle("微信支付", for: .normal)
            wechatPayBtn.setTitleColor(.white, for: .normal)
            wechatPayBtn.backgroundColor = .mainColor
            wechatPayBtn.titleLabel?.font = .regular_12
            wechatPayBtn.layer.cornerRadius = 15
            wechatPayBtn.layer.masksToBounds = true
            wechatPayBtn.setBackgroundImage(UIImage(color: .splitLineLight), for: .disabled)
        }
    }
    @IBOutlet weak var tips: UILabel! {
        didSet {
            tips.font = .regular_12
            tips.textColor = .init(hex: "#666666")
            tips.isHidden = true
        }
    }
    
    @IBAction func medicalInsurancePay(_ sender: Any) {
        submit(type: .medicalInsurancePay)
    }
    @IBAction func wechatPay(_ sender: Any) {
        submit(type: .wechatPay)
    }
    
    func submit(type: NewAdvisoryGenerateOrderEnum) {
        if payTimeout {
            HUDTool.show(message: "已超时，不能支付")
        } else {
            delegate?.didTappedPayAction(type: type, view: self)
        }
    }
}
